#ifndef TTIMER_H
#define TTIMER_H

#include <functional>
#include <chrono>
#include <thread>
#include <atomic>
#include <memory>
#include <mutex>
#include <condition_variable>

class TTimer
{
public:
    TTimer(size_t interval, bool isRepeat);
    ~TTimer();

    void start(std::function<void()> task);
    void stop();

    bool isExpired();

private:
    std::atomic<bool> _expired; // timer stopped status
    std::atomic<bool> _try_to_expire; // timer is in stop process
    std::mutex _mutex;
    std::condition_variable _expired_cond;

    /* 仅初始化时使用，不需要加锁 */
    size_t m_interval;
    bool m_isRepeat;
};

#endif // TTIMER_H
